#include "openfile.h"
#include "ui_openfile.h"

#include "newfile.h"
#include "mainwindow.h"

#include <QSqlQuery>
#include <QDate>
#include <QTableWidgetItem>
#include <QMessageBox>


static QString deletedId;

OpenFile::OpenFile(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::OpenFile)
{
    ui->setupUi(this);
    ui->tableWidget->horizontalHeader()->setVisible(true);
    QSqlQuery query;
    query.exec("select * from patient");
    t = 0;
    while (query.next())
    {
        ui->tableWidget->insertRow(t);
        t++;
    }
    query.exec("select * from patient");
    int i = 0;
    while (query.next())
    {
       QString rowIndex;
       if( i <9 ) rowIndex = "00" + QString::number(i+1);
       else if( (i >= 9 ) && ( i< 99)) rowIndex = "0" + QString::number(i+1);
       else rowIndex = QString::number(i+1);
       ui->tableWidget->setItem(i,0,new QTableWidgetItem(rowIndex));
       QDate birthday = QDate::fromString(query.value(7).toString(),"yyyy/MM/dd");
       int age = QDate::currentDate().year() - birthday.year();
       ui->tableWidget->setItem(i,0,new QTableWidgetItem(rowIndex));
       ui->tableWidget->setItem(i,1,new QTableWidgetItem(query.value(1).toString()));
       ui->tableWidget->setItem(i,2,new QTableWidgetItem(QString::number(age)));
       ui->tableWidget->setItem(i,3,new QTableWidgetItem(query.value(7).toString()));
       ui->tableWidget->setItem(i,4,new QTableWidgetItem(query.value(10).toString()));
       ui->tableWidget->setItem(i,5,new QTableWidgetItem("————"));
       ui->tableWidget->setItem(i,6,new QTableWidgetItem(query.value(6).toString()));
       ui->tableWidget->setItem(i,7,new QTableWidgetItem(query.value(0).toString()));
       ui->tableWidget->item(i,5)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
       i++;
     }
}

OpenFile::~OpenFile()
{
    delete ui;
}


void OpenFile::on_tableWidget_itemClicked(QTableWidgetItem *item)
{
    ui->deleteBtn->setEnabled(true);
    ui->certainBtn->setEnabled(true);
    ui->item_label->setText(item->text());
    ui->item_label->setVisible(false);
    QList<QTableWidgetItem*> items = ui->tableWidget->selectedItems();
    deletedId = items.at(7)->text();
}

void OpenFile::on_deleteBtn_clicked()
{
    //删除记录操作
    int isDeleted = QMessageBox::warning(this,"注意","删除后不可恢复\n   确定删除？","确定","取消");
    if( isDeleted == QObject::tr("确定").toInt())
    {
        QSqlQuery query;
        query.exec("select * from patient");
        query.next();
        QString deleteStr = QString("delete from patient where ID = \"%1\"").arg(deletedId);
        if(query.exec(deleteStr))
        {
            int yes = QMessageBox::information(this,"提示","删除成功","确定");
            if( yes == QObject::tr("确定").toInt())
            {
                ui->tableWidget->clearContents();
                query.exec("select * from patient");
                int i = 0;
                while (query.next())
                {
                    QString rowIndex;
                    if( i <9 ) rowIndex = "00" + QString::number(i+1);
                    else if( (i >= 9 ) && ( i< 99)) rowIndex = "0" + QString::number(i+1);
                    else rowIndex = QString::number(i+1);
                    ui->tableWidget->setItem(i,0,new QTableWidgetItem(rowIndex));
                    QDate birthday = QDate::fromString(query.value(7).toString(),"yyyy/MM/dd");
                    int age = QDate::currentDate().year() - birthday.year();
                    ui->tableWidget->setItem(i,0,new QTableWidgetItem(rowIndex));
                    ui->tableWidget->setItem(i,1,new QTableWidgetItem(query.value(1).toString()));
                    ui->tableWidget->setItem(i,2,new QTableWidgetItem(QString::number(age)));
                    ui->tableWidget->setItem(i,3,new QTableWidgetItem(query.value(7).toString()));
                    ui->tableWidget->setItem(i,4,new QTableWidgetItem(query.value(10).toString()));
                    ui->tableWidget->setItem(i,5,new QTableWidgetItem("————"));
                    ui->tableWidget->setItem(i,6,new QTableWidgetItem(query.value(6).toString()));
                    ui->tableWidget->setItem(i,7,new QTableWidgetItem(query.value(0).toString()));
                    ui->tableWidget->item(i,5)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
                    i++;
                }
            }
            ui->deleteBtn->setEnabled(false);
        }
        else QMessageBox::information(this,"提示","删除失败");
    }
    else ui->deleteBtn->setEnabled(false);
}


void OpenFile::on_addBtn_clicked()
{
    newFile = new NewFile;
    connect(newFile,SIGNAL(addRecord(QString,QString,QString,QString,QString,QString)),
            this,SLOT(slot_addRecord(QString,QString,QString,QString,QString,QString)));
    newFile->show();
}


void OpenFile::slot_addRecord(QString newName, QString newAge, QString newBirthday,
                              QString newAsseessDay, QString newCheckTimes, QString newId)
{
    QSqlQuery query;
    query.exec("select * from patient");
    query.next();
    int rows = ui->tableWidget->rowCount();
    ui->tableWidget->setRowCount(rows+1);
    QString rowIndex ;
    if( rows<9 )
        rowIndex = "00"+QString::number(rows+1);
    else if((rows >= 9) && (rows <99))
        rowIndex = "0" + QString::number(rows+1);
    else rowIndex = QString::number(rows+1);
    ui->tableWidget->setItem(rows,0,new QTableWidgetItem(rowIndex));
    ui->tableWidget->setItem(rows,1,new QTableWidgetItem(newName));
    ui->tableWidget->setItem(rows,2,new QTableWidgetItem(newAge));
    ui->tableWidget->setItem(rows,3,new QTableWidgetItem(newBirthday));
    ui->tableWidget->setItem(rows,4,new QTableWidgetItem(newAsseessDay));
    ui->tableWidget->setItem(rows,5,new QTableWidgetItem("————"));
    ui->tableWidget->setItem(rows,6,new QTableWidgetItem(newCheckTimes));
    ui->tableWidget->setItem(rows,7,new QTableWidgetItem(newId));
    ui->tableWidget->item(rows,5)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
}



void OpenFile::on_tableWidget_itemDoubleClicked(QTableWidgetItem *item)
{
    ui->item_label->setText(item->text());
    ui->item_label->setVisible(false);
    QList<QTableWidgetItem*> items = ui->tableWidget->selectedItems();
    QString itemId = items.at(7)->text();
    details = new Details(itemId);
    connect(details,SIGNAL(changeRecord()),this,SLOT(slot_changeRecord()));
    details->show();
}

void OpenFile::on_certainBtn_clicked()
{
    QList<QTableWidgetItem*> items = ui->tableWidget->selectedItems();
    QString itemId = items.at(7)->text();
    details = new Details(itemId);
    connect(details,SIGNAL(changeRecord()),this,SLOT(slot_changeRecord()));
    details->show();
}

void OpenFile::slot_changeRecord()
{
    QSqlQuery query;
    ui->tableWidget->clearContents();
    query.exec("select * from patient");
    int i = 0;
    while (query.next())
    {
       QString rowIndex;
       if( i <9 ) rowIndex = "00" + QString::number(i+1);
       else if( (i >= 9 ) && ( i< 99)) rowIndex = "0" + QString::number(i+1);
       else rowIndex = QString::number(i+1);
       ui->tableWidget->setItem(i,0,new QTableWidgetItem(rowIndex));
       QDate birthday = QDate::fromString(query.value(7).toString(),"yyyy/MM/dd");
       int age = QDate::currentDate().year() - birthday.year();
       ui->tableWidget->setItem(i,0,new QTableWidgetItem(rowIndex));
       ui->tableWidget->setItem(i,1,new QTableWidgetItem(query.value(1).toString()));
       ui->tableWidget->setItem(i,2,new QTableWidgetItem(QString::number(age)));
       ui->tableWidget->setItem(i,3,new QTableWidgetItem(query.value(7).toString()));
       ui->tableWidget->setItem(i,4,new QTableWidgetItem(query.value(10).toString()));
       ui->tableWidget->setItem(i,5,new QTableWidgetItem("————"));
       ui->tableWidget->setItem(i,6,new QTableWidgetItem(query.value(6).toString()));
       ui->tableWidget->setItem(i,7,new QTableWidgetItem(query.value(0).toString()));
       ui->tableWidget->item(i,5)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
       i++;
     }
}
